package org.springframework.aop.framework.autoproxy;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Before;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectInstanceFactory;
import org.springframework.aop.aspectj.AspectJAfterReturningAdvice;
import org.springframework.aop.aspectj.AspectJAroundAdvice;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.aspectj.AspectJMethodBeforeAdvice;
import org.springframework.aop.aspectj.SingletonAspectInstanceFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;

/****
 * 高级切面转换成初级切面
 * @author Administrator
 *
 */
public class A17_2 {
  
	public static void main(String[] args) {
		 test2();
	}
	
	
	/****
	 * @before转换成初级类
	 */
	public static void test1() {
		 AspectInstanceFactory factory = new SingletonAspectInstanceFactory(new Aspect());
	        // 高级切面转低级切面类
	        List<Advisor> list = new ArrayList<>();
	        for (Method method : Aspect.class.getDeclaredMethods()) {//通过切面类得到里边的方法
	            if (method.isAnnotationPresent(Before.class)) {//判断是否有@before的注解
	                // 解析切点
	                String expression = method.getAnnotation(Before.class).value();//得到注解表达式
	                AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
	                pointcut.setExpression(expression);//创建切点类 把表达式设置进去
	                //  创建通知类
	                AspectJMethodBeforeAdvice advice = new AspectJMethodBeforeAdvice(method, pointcut, factory);
	                // 创建切面
	                Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);
	                list.add(advisor);
	            }
	        }
	        for (Advisor advisor : list) {
	            System.out.println(advisor);
	        }
	}
	
	public static void test2() {
		 AspectInstanceFactory factory = new SingletonAspectInstanceFactory(new Aspect());
	        // 高级切面转低级切面类
	        List<Advisor> list = new ArrayList<>();
	        for (Method method : Aspect.class.getDeclaredMethods()) {//通过切面类得到里边的方法
	            if (method.isAnnotationPresent(Before.class)) {//判断是否有@before的注解
	                // 解析切点
	                String expression = method.getAnnotation(Before.class).value();//得到注解表达式
	                AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
	                pointcut.setExpression(expression);//创建切点类 把表达式设置进去
	                //  创建通知类
	                AspectJMethodBeforeAdvice advice = new AspectJMethodBeforeAdvice(method, pointcut, factory);
	                // 创建切面
	                Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);
	                list.add(advisor);
	            }else if (method.isAnnotationPresent(AfterReturning.class)) {
	                // 解析切点
	                String expression = method.getAnnotation(AfterReturning.class).value();
	                AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
	                pointcut.setExpression(expression);
	                // 通知类
	                AspectJAfterReturningAdvice advice = new AspectJAfterReturningAdvice(method, pointcut, factory);
	                // 切面
	                Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);
	                list.add(advisor);
	            } else if (method.isAnnotationPresent(Around.class)) {
	                // 解析切点
	                String expression = method.getAnnotation(Around.class).value();
	                AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
	                pointcut.setExpression(expression);
	                // 通知类
	                AspectJAroundAdvice advice = new AspectJAroundAdvice(method, pointcut, factory);
	                // 切面
	                Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);
	                list.add(advisor);
	            }
	        }
	        for (Advisor advisor : list) {
	            System.out.println("----"+advisor);
	        }
	}
	
	
	static class Aspect {
        @Before("execution(* foo())")
        public void before1() {
            System.out.println("before1");
        }

        @Before("execution(* foo())")
        public void before2() {
            System.out.println("before2");
        }

        public void after() {
            System.out.println("after");
        }

        public void afterReturning() {
            System.out.println("afterReturning");
        }

        public void afterThrowing() {
            System.out.println("afterThrowing");
        }

        public Object around(ProceedingJoinPoint pjp) throws Throwable {
            try {
                System.out.println("around...before");
                return pjp.proceed();
            } finally {
                System.out.println("around...after");
            }
        }
    }

    static class Target {
        public void foo() {
            System.out.println("target foo");
        }
    }
}
